/*
 * Daniel R Padilla
 *
 * Copyright (c) 2007, Daniel R Padilla
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.databasefacade.table.row;


import java.sql.Connection;
import java.util.Iterator;

import org.databasefacade.SessionTracker;
import org.databasefacade.table.Table;
import org.databasefacade.table.column.AliasedColumnList;
import org.databasefacade.table.column.Column;


public class Row  {
    protected Table table= null;
    protected AliasedColumnList columns = null;
    protected Row oldRow = null;
    
    public Row(Table table) {
    	this.table = table;
    	this.columns = new AliasedColumnList(table.getDBHelper().getTranslator());
    }
    
    /**
     * Make a copy of a row
     */
    public Row(Row row, boolean cloneValue) {
        if (row != null) {
            table = row.getTable();
            for (int i = 0; i < row.numberOfColumns(); i++) {
                Column cr = row.getColumn(i);
                if (cr != null) {
                    addColumn(cr);
                }
            }
        }
    }
	

    public Table getTable() {
    	return table;
    }
    
    public String getTableName() {
    	return table.getTableName();
    }
    
    /**
     * Get the number of columns
     */
    public int numberOfColumns() {
        return columns.size();
    }
	
    public void addColumn(Column col) {
        columns.addAliasedColumn(col);
    }
    	
    public Column getColumn(int i) {
        return columns.getColumn(i);
    }

    public Column getColumn(String colName) {
        return columns.getColumn(colName);
    }

    public Iterator getColumnNames() {
        return columns.getColumnAliases();
    }

           
    /**
     * I am not going to be nice the last record saved is the 
     * record that will be in the database. Being nice can be
     * implemented later.
     */
    public void dbSave(SessionTracker session) throws Exception { 

        /**
         * Make sure that the trigger an saving happens as a transaction
         */
        Connection conn = table.getDBHelper().getConnection();

        try {
            conn.setAutoCommit(false);
            dbSave(conn, session);
            if (conn.getAutoCommit() == false) {
                conn.commit();
            }
            conn.close();
        } catch (Exception e) {
            conn.rollback();
            conn.close();
            throw e;
        }
    }
    
    public void dbSave(Connection conn, SessionTracker session) throws Exception {
    }
    
    public void dbDelete(Connection conn, SessionTracker session) throws Exception {
    }
    
    /**
     * Update the row instance
     */
    private void dbUpdate(Connection conn, SessionTracker session) throws Exception {
    }

    private void dbInsert(Connection conn, SessionTracker session) throws Exception {
    }
  
    public void setOldRow(Row old) {
        this.oldRow = old;
    }
    
    public String toString() {
    	String s="";
    	for (int i=0; i < columns.size(); i++) {
    		s += "\n   " + columns.getAliasedColumn(i).toString();
    	}
    	return s;
    }
    

}
